package com.example.springbootsqlite.controller;

import cn.hutool.core.io.FileUtil;
import com.example.springbootsqlite.BinBashUtil;
import com.example.springbootsqlite.entity.Doc;
import com.example.springbootsqlite.entity.DocA;
import com.example.springbootsqlite.service.DocAService;
import com.example.springbootsqlite.service.DocService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * @author maigao
 * @date 2023/2/4 17:40
 * @description:
 */
@RestController
public class DocController {
    @Autowired
    private DocService docService;
    @Autowired
    private DocAService docAService;

    @GetMapping(value = "/id")
    public Doc get(Integer id) throws Exception {
        return docService.getDocById(id);
    }

    @GetMapping(value = "/list")
    public List<Doc> list() {
        return docService.list();
    }

    @GetMapping(value = "/a/list")
    public List<DocA> listA() {
        return docAService.list();
    }

    /**
     * 推送消息
     * 接收外部推送消息
     * @param infoMsg 消息
     * @param request 请求对象
     * @return true 成功
     */
    @PostMapping(value = "/push")
    public SysInfoMsg push(SysInfoMsg infoMsg, HttpServletRequest request) throws IOException {

        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        //如果有多个文件则需要获取指定的文件字段
        MultipartFile file = multipartRequest.getFile("file");
        Assert.notNull(file, "推送文件为空！");

        String filename = file.getOriginalFilename();
        if (filename != null && StringUtils.isEmpty(infoMsg.getFileType())) {
            infoMsg.setFileType(filename.substring(filename.lastIndexOf(".") + 1));
        }

        infoMsg.setUrl(filename);
        infoMsg.setFileSize(file.getSize());
        infoMsg.setNoticeScope(StringUtils.isEmpty(infoMsg.getProjectIds()) ? 1 : 0);

        String ab = "/data/jeecg/data/property/infoMsgFile/";
        String path = ab + filename;
        FileUtil.writeBytes(file.getBytes(), new File(path));

        String s = "ffmpeg -i " + path + " -c:v libx264 -hls_time 60 -hls_list_size 0 -c:a aac -strict -2 -f hls " + ab + "a.m3u8";
        CompletableFuture.runAsync(() -> BinBashUtil.exec(s, "分割视频"));
        return infoMsg;
    }


    class SysInfoMsg {

        private String title;


        private String source;


        private Long fileSize;


        private String url;


        private String fileType;


        private String content;

        private Integer noticeScope;

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getSource() {
            return source;
        }

        public void setSource(String source) {
            this.source = source;
        }

        public Long getFileSize() {
            return fileSize;
        }

        public void setFileSize(Long fileSize) {
            this.fileSize = fileSize;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public String getFileType() {
            return fileType;
        }

        public void setFileType(String fileType) {
            this.fileType = fileType;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }

        public Integer getNoticeScope() {
            return noticeScope;
        }

        public void setNoticeScope(Integer noticeScope) {
            this.noticeScope = noticeScope;
        }

        public String getProjectIds() {
            return projectIds;
        }

        public void setProjectIds(String projectIds) {
            this.projectIds = projectIds;
        }

        private String projectIds;
    }
}
